package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressMapper;
import com.sky.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressMapper addressMapper;

    @Override
    public List<AddressBook> findAddress(AddressBook addressBook) {
        addressBook.setId(BaseContext.getCurrentId());
        List<AddressBook> list = addressMapper.getAddressList(addressBook);
        return list;
    }

    @Override
    public void addAddress(AddressBook address) {
        address.setUserId(BaseContext.getCurrentId());
        address.setIsDefault(0);
        addressMapper.addAddressByUserId(address);
    }

    @Override
    public void setDefault(AddressBook address) {
        address.setUserId(BaseContext.getCurrentId());
        address.setIsDefault(0);
        addressMapper.updateAllAddress(address);
        address.setIsDefault(1);
        addressMapper.updateAddressById(address);
    }

    @Override
    public AddressBook findAddressById(long id) {
        return addressMapper.findAddressById(id);
    }

    @Override
    public void updateById(AddressBook address) {
        addressMapper.updateAddressById(address);
    }

    @Override
    public void deleteAddress(AddressBook address) {
        addressMapper.deleteById(address);
    }


}
